<template>
  <div id="userRegister">
    <h2 style="margin-bottom: 16px">用户注册</h2>
    <a-form
      :model="form"
      label-align="left"
      auto-label-width
      @submit="handleSubmit"
      class="loginForm"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input
          v-model="form.userAccount"
          tooltip="账号名长度要大于4位"
          placeholder="请输入账号"
        />
      </a-form-item>
      <a-form-item field="userPassword" tooltip="密码不少于8位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item
        field="checkPassword"
        tooltip="密码不少于8位"
        label="确认密码"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="再次输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 120px"
          >注册
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<style scoped>
#userRegister {
}

#userRegister .loginForm {
  max-width: 480px;
  margin: 0 auto;
}
</style>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserRegisterRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const router = useRouter();
const store = useStore();
/**
 * 表单数据
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as UserRegisterRequest);
/**
 * 提交表单
 */
const handleSubmit = async () => {
  const res = await UserControllerService.userRegisterUsingPost(form); //将用户信息发送给后台
  if (res.code === 0) {
    message.success("注册成功,即将返回登录页面");
    setTimeout(() => {
      router.push({
        path: "/user/login",
        replace: true,
      });
    }, 3000);
  } else {
    message.error("注册失败," + res.message);
  }
};
</script>
